package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.mall.domain.MallOrder;
import com.ruoyi.mall.domain.MallOrderDetail;
import com.ruoyi.mall.domain.bo.MallOrderDetailBo;
import com.ruoyi.mall.domain.vo.MallOrderDetailVo;
import com.ruoyi.mall.domain.vo.MallOrderVo;
import com.ruoyi.mall.mapper.MallOrderDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.MallOrderBo;
import com.ruoyi.mall.mapper.MallOrderMapper;
import com.ruoyi.mall.service.IMallOrderService;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分商城订单主Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class MallOrderServiceImpl implements IMallOrderService {

    private final MallOrderMapper baseMapper;

    private final MallOrderDetailMapper mallOrderDetailMapper;

    /**
     * 查询积分商城订单主
     */
    @Override
    public MallOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public List<MallOrderDetailVo> queryOrderDetailByOrderId(String orderId)
    {
        MallOrderDetailBo query = new MallOrderDetailBo();
        query.setScoreMallOrderId(orderId);
        LambdaQueryWrapper<MallOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(orderId != null, MallOrderDetail::getScoreMallOrderId, orderId);
        return mallOrderDetailMapper.selectVoList(lqw);
    }

    @Override
    public List<MallOrderVo> queryOrderByCustomerId(Long customerId)
    {
        MallOrderBo query = new MallOrderBo();
        query.setCustomerId(customerId);
        LambdaQueryWrapper<MallOrder> lqw = buildQueryWrapper(query);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询积分商城订单主列表
     */
    @Override
    public TableDataInfo<MallOrderVo> queryPageList(MallOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallOrder> lqw = buildQueryWrapper(bo);
        Page<MallOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分商城订单主列表
     */
    @Override
    public List<MallOrderVo> queryList(MallOrderBo bo) {
        LambdaQueryWrapper<MallOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MallOrder> buildQueryWrapper(MallOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getScoreMallOrderId()), MallOrder::getScoreMallOrderId, bo.getScoreMallOrderId());
        lqw.eq(bo.getCustomerId() != null, MallOrder::getCustomerId, bo.getCustomerId());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), MallOrder::getCustomerName, bo.getCustomerName());
        lqw.eq(bo.getTotalScoreAmount() != null, MallOrder::getTotalScoreAmount, bo.getTotalScoreAmount());
        lqw.eq(bo.getTotalAmount() != null, MallOrder::getTotalAmount, bo.getTotalAmount());
        lqw.eq(bo.getTotalMoney() != null, MallOrder::getTotalMoney, bo.getTotalMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getTransactionNo()), MallOrder::getTransactionNo, bo.getTransactionNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPayUrl()), MallOrder::getPayUrl, bo.getPayUrl());
        lqw.eq(bo.getPayTime() != null, MallOrder::getPayTime, bo.getPayTime());
        lqw.eq(bo.getPayType() != null, MallOrder::getPayType, bo.getPayType());
        lqw.eq(bo.getStatus() != null, MallOrder::getStatus, bo.getStatus());
        lqw.eq(bo.getYn() != null, MallOrder::getYn, bo.getYn());
        return lqw;
    }

    /**
     * 新增积分商城订单主
     */
    @Override
    public Boolean insertByBo(MallOrderBo bo) {
        MallOrder add = BeanUtil.toBean(bo, MallOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改积分商城订单主
     */
    @Override
    public Boolean updateByBo(MallOrderBo bo) {
        MallOrder update = BeanUtil.toBean(bo, MallOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分商城订单主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
